package com.kuaimacode.kframework.mybatis.models.user;

import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import com.kuaimacode.kframework.common.Constant;
import com.kuaimacode.kframework.exception.ErrorEnum;
import com.kuaimacode.kframework.exception.ServerInternalErrorException;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;

import java.math.BigDecimal;
import java.util.Date;

@Data
@TableName("user")
public class User implements java.io.Serializable {
    /**
     *
     */
    @TableField(exist = false)
    private static final long serialVersionUID = 1L;

    @ApiModelProperty(value = "用户ID")
    @TableId(value = "id", type = IdType.AUTO)
    private Long id;

    @ApiModelProperty(value = "用户名")
    @TableField("username")
    private String username;

    @ApiModelProperty(value = "密码")
    @TableField("password")
    private String password;

    @ApiModelProperty(value = "电子邮箱")
    @TableField("email")
    private String email;

    @ApiModelProperty(value = "手机号")
    @TableField("phone")
    private String phone;

    @ApiModelProperty(value = "注册时间")
    @TableField("register_date")
    private Date registerDate;

    @ApiModelProperty(value = "最后登录时间")
    @TableField("last_login_date")
    private Date lastLoginDate;

    @ApiModelProperty(value = "最后一次密码重设时间")
    @TableField("lastpasswordresetdate")
    private Date lastpasswordresetDate;

    @ApiModelProperty(value = "是否启用")
    @TableField("enabled")
    private Integer enabled;

    @ApiModelProperty(value = "微信OpenID")
    @TableField("open_id")
    private String openId;

    @ApiModelProperty(value = "微信unionId")
    @TableField("union_id")
    private String unionId;

    @ApiModelProperty(value = "昵称")
    @TableField("nickName")
    private String nickname;

    @ApiModelProperty(value = "性别")
    @TableField("gender")
    private Short gender;

    @ApiModelProperty(value = "语言")
    @TableField("language")
    private String language;

    @ApiModelProperty(value = "城市")
    @TableField("city")
    private String city;

    @ApiModelProperty(value = "省份")
    @TableField("province")
    private String province;

    @ApiModelProperty(value = "国家/地区")
    @TableField("country")
    private String country;

    @ApiModelProperty(value = "用户头像URL")
    @TableField("avatar_url")
    private String avatarUrl;

    @ApiModelProperty(value = "验证码")
    @TableField("sms_code")
    private String smsCode;

    @ApiModelProperty(value = "短信发送时间")
    @TableField("sms_time")
    private Date smsTime;

    @ApiModelProperty(value = "用户代收款账户余额")
    @TableField("collect_Bal")
    private BigDecimal collectBal;

    @ApiModelProperty(value = "用户积分")
    @TableField("points")
    private Short points;

    @ApiModelProperty(value = "用户VIP级别")
    @TableField("vip_level")
    private Short vipLevel;

    @ApiModelProperty(value = "是否同意协议 0 否 1 是")
    @TableField("is_protocol")
    private Short isProtocol;

    @ApiModelProperty(value = "个人签名")
    @TableField("signature")
    private String signature;

    @ApiModelProperty(value = "是否已实名 0 否 1 是")
    @TableField("is_certifacate")
    private Byte isCertifacate;

    @ApiModelProperty(value = "是否同意骑手协议 0 否 1 是")
    @TableField("is_rider")
    private Integer isRider;

    @ApiModelProperty(value = "是否已认证为师傅 0 否 1 认证中 2已认证")
    @TableField("is_master")
    private Integer isMaster;


    @ApiModelProperty(value = "师傅账号状态 0登出，登入")
    @TableField("master_status")
    private String masterStatus;

    @ApiModelProperty(value = "账户余额")
    @TableField("account_balance")
    private BigDecimal accountBalance;


    @ApiModelProperty(value = "合作商家地址")
    @TableField("partner_address")
    private String partnerAddress;

    @ApiModelProperty(value = "合作商家电话")
    @TableField("partner_phone")
    private String partnerPhone;

    @ApiModelProperty(value = "合作商家地址纬度")
    @TableField("partner_latitude")
    private String partnerLatitude;

    @ApiModelProperty(value = "合作商家地址经度")
    @TableField("partner_longitude")
    private String partnerLongitude;

    @ApiModelProperty(value = "合作商家名称")
    @TableField("partner_name")
    private String partnerName;

    @ApiModelProperty(value = "合作商家头像")
    @TableField("partner_avatar")
    private String partnerAvatar;

    @ApiModelProperty(value = "合作商家文案")
    @TableField("partner_remark")
    private String partnerRemark;

    @ApiModelProperty(value = "是否合作商家 1-是，0-否")
    @TableField("is_partner")
    private Integer isPartner;

    @ApiModelProperty(value = "合作商家单笔基础费用")
    @TableField("partner_amount")
    private BigDecimal partnerAmount;

    @ApiModelProperty(value = "合作商家协议")
    @TableField("partner_protocol")
    private String partnerProtocol;

    @ApiModelProperty(value = "合作商家申请状态")
    @TableField("partner_status")
    private Byte partnerStatus;

    @ApiModelProperty(value = "拒绝原因")
    @TableField("partner_reason")
    private String partnerReason;

    @ApiModelProperty(value = "分站ID")
    @TableField("agent_id")
    private String agentId;

    @ApiModelProperty(value = "抽成比例")
    @TableField("partner_scale")
    private BigDecimal partnerScale;

    @ApiModelProperty(value = "合作商家所属分站ID")
    @TableField("partner_agent")
    private String partnerAgent;

    @TableField("token")
    private String token;

    @TableField("form_id")
    private String formId;

    @ApiModelProperty(value = "用户订单数")
    @TableField(exist = false)
    private Long userOrderCount;

    @ApiModelProperty(value = "最后登录IP")
    @TableField("last_login_ip")
    private String lastLoginIp;

    @ApiModelProperty(value = "注册IP")
    @TableField("register_ip")
    private String registerIp;

    @ApiModelProperty(value = "派单缓存订单id")
    @TableField("popup_paidan")
    private String popupPaidan;

    @ApiModelProperty(value = "取消缓存订单id")
    @TableField("popup_quxiao")
    private String popupQuxiao;

    /**
     * 是否为合作商家
     *
     * @return
     */
    public boolean isAgreePartner() {
        return isPartner != null && isPartner == 1;
    }

    /**
     * 是否为师傅
     *
     * @return
     */
    public boolean isOneMaster() {
        return isMaster != null && isMaster == 2;
    }

    /**
     * 是否已实名
     *
     * @return
     */
    public boolean isCertificated() {
        return isCertifacate != null && isCertifacate == 1;
    }

    /**
     * 合作商家申请中
     */
    public void applyPartner(String agentId) {
        if (partnerStatus.byteValue() == Constant.PartnerStatusEnum.AGREED.getCode()
                || partnerStatus.byteValue() == Constant.PartnerStatusEnum.APPLYING.getCode()) {
            throw new ServerInternalErrorException(ErrorEnum.PARTNER_APPLYING_OR_AGREED);
        }
        isPartner = 0;
        partnerStatus = Constant.PartnerStatusEnum.APPLYING.getCode();
        this.partnerAgent = agentId;
    }

    /**
     * 同意合作商家
     */
    public void agreePartner(BigDecimal scale) {
        isPartner = 1;
        partnerScale = scale;
        partnerStatus = Constant.PartnerStatusEnum.AGREED.getCode();
    }

    /**
     * 拒绝合作商家
     */
    public void rejectPartner(String reason) {
        this.partnerReason = reason;
        isPartner = 0;
        partnerStatus = Constant.PartnerStatusEnum.REJECTED.getCode();
    }
}